package com.example.backend.service;

import com.example.backend.entity.*;
import com.example.backend.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DingDanService {
    @Autowired
    private DingDanMapper dingDanMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RiderMapper riderMapper;



    @Autowired
    private ShopMapper shopMapper;


    @Autowired
    private UserAdressMapper userAdressMapper;


    @Autowired
    private DingDanDetailMapper dingDanDetailMapper;


    public int addDingDan(DingDan dingDan){
        dingDan.setState("0");
        return dingDanMapper.addDingDan(dingDan);
    }
    public int updateDingDan(DingDan dingDan){
        return dingDanMapper.updateDingDan(dingDan);
    }
    public int deleteDingDanById(String id){
        return dingDanMapper.deleteDingDanById( id);
    }
    public DingDan selectDingDanById(String id){
        return dingDanMapper.selectDingDanById(id);
    }

    public DingDan selectDingDanByDingDanNum(String dingdan_num){
        return dingDanMapper.selectDingDanByDingDanNum(dingdan_num);
    }

    /**
     * 分页查询订单信息类
     * @return
     */
    public Map<String, Object> selectDingDans(DingDan dingdan, int pageNum, int pageSize){
        System.out.println(pageNum);
        // 使用传入的分页参数启动分页
        PageHelper.startPage(pageNum, pageSize);
        //先查订单表
        ArrayList<DingDan> dingDans = dingDanMapper.selectDingDans(dingdan);
        // 创建 PageInfo
        PageInfo<DingDan> pageInfo = new PageInfo<>(dingDans);
        //
        ArrayList<DingDanMessage> dingDanMessages = new ArrayList<>();
        //将订单表数据转换成订单信息(相当于多表查询)
        for (int i = 0; i < dingDans.size(); i++) {
            DingDanMessage dingDanMessage = new DingDanMessage();

            // 获取订单信息并赋值订单信息类

            User user = userMapper.selectUserById(dingDans.get(i).getUser_id());
            if (user != null) {
                dingDanMessage.setName(user.getYhm());
                dingDanMessage.setPhone(user.getSjh());
            } else {
                dingDanMessage.setName("未知用户");
                dingDanMessage.setPhone("未知电话");
            }

            Shop shop = shopMapper.selectShopById(dingDans.get(i).getShop_id());
            if (shop != null) {
                dingDanMessage.setShop_name(shop.getShopname());
                dingDanMessage.setGratuity(shop.getGratuity());
            } else {
                dingDanMessage.setShop_name("未知商家");
            }

            Rider rider = riderMapper.selectRiderById(dingDans.get(i).getRider_id());
            if (rider != null) {
                dingDanMessage.setRider_name(rider.getName());
            } else {
                dingDanMessage.setRider_name("未知骑手");
            }

            UserAdress address = userAdressMapper.selectById(dingDans.get(i).getAddress_id());
            if (address != null) {
                dingDanMessage.setAddress(address.getAddress());
            } else {
                dingDanMessage.setAddress("未知地址");
            }
            dingDanMessage.setDingdan_num(dingDans.get(i).getDingdan_num());

            dingDanMessage.setDishes_name(dingDanDetailMapper.selectDingDanDetailsByDingDanNum(dingDans.get(i).getDingdan_num()));

            dingDanMessage.setPrice(dingDans.get(i).getMoney());


            dingDanMessage.setState(dingDans.get(i).getState());

            dingDanMessage.setTime(dingDans.get(i).getTime());

            dingDanMessage.setEnd_time(dingDans.get(i).getEnd_time());


            dingDanMessage.setRemark(dingDans.get(i).getRemark());

            dingDanMessages.add(dingDanMessage);
        }
        
        // 构建包含数据和分页信息的响应
        Map<String, Object> result = new HashMap<>();
        result.put("list", dingDanMessages);          // 当前页的数据列表
        result.put("total", pageInfo.getTotal());       // 总记录数
        result.put("pages", pageInfo.getPages());       // 总页数
        
        return result;
    }

    /**
     * 查询订单
     * @return
     */
    public ArrayList<DingDan> selectDingDans(DingDan dingdan){
        //先查订单表
        ArrayList<DingDan> dingDans = dingDanMapper.selectDingDans(dingdan);
        return dingDans;
    }

    /**
     * 获取订单信息类
     * @return
     */
    public ArrayList<DingDanMessage> selectDingDansNotPage(DingDan dingdan){

        //先查订单表
        ArrayList<DingDan> dingDans = dingDanMapper.selectDingDans(dingdan);

        //
        ArrayList<DingDanMessage> dingDanMessages = new ArrayList<>();
        //将订单表数据转换成订单信息(相当于多表查询)
        for (int i = 0; i < dingDans.size(); i++) {
            DingDanMessage dingDanMessage = new DingDanMessage();

            // 获取订单信息并赋值订单信息类

            User user = userMapper.selectUserById(dingDans.get(i).getUser_id());
            if (user != null) {
                dingDanMessage.setName(user.getYhm());
            } else {
                dingDanMessage.setName("未知用户");
            }

            Shop shop = shopMapper.selectShopById(dingDans.get(i).getShop_id());
            if (shop != null) {
                dingDanMessage.setShop_name(shop.getShopname());
                dingDanMessage.setGratuity(shop.getGratuity());
            } else {
                dingDanMessage.setShop_name("未知商家");
            }

            Rider rider = riderMapper.selectRiderById(dingDans.get(i).getRider_id());
            if (rider != null) {
                dingDanMessage.setRider_name(rider.getName());
            } else {
                dingDanMessage.setRider_name("未知骑手");
            }

            UserAdress address = userAdressMapper.selectById(dingDans.get(i).getAddress_id());
            if (address != null) {
                dingDanMessage.setAddress(address.getAddress());
                dingDanMessage.setPhone(address.getPhone());
            } else {
                dingDanMessage.setPhone("未知电话");
                dingDanMessage.setAddress("未知地址");
            }
            dingDanMessage.setDingdan_num(dingDans.get(i).getDingdan_num());

            dingDanMessage.setDishes_name(dingDanDetailMapper.selectDingDanDetailsByDingDanNum(dingDans.get(i).getDingdan_num()));

            dingDanMessage.setPrice(dingDans.get(i).getMoney());


            dingDanMessage.setState(dingDans.get(i).getState());

            dingDanMessage.setTime(dingDans.get(i).getTime());

            dingDanMessage.setEnd_time(dingDans.get(i).getEnd_time());


            dingDanMessage.setRemark(dingDans.get(i).getRemark());

            dingDanMessages.add(dingDanMessage);
        }

        // 构建包含数据和分页信息的响应

        return dingDanMessages;
    }

    /**
     * 根据订单编号获取订单详情（菜品信息）
     */
    public List<DingDanDetail> getDingDanDetailsByDingDanNum(String dingdan_num) {
        return dingDanDetailMapper.selectDingDanDetailsByDingDanNum(dingdan_num);
    }
}
